from check_privilege import *
#import Reporting.GenRpt_test as rpt_m
import Reports.headfile as rpt_m

from datetime import datetime,date
from operator import itemgetter


# rpt_conf = {
	# '1': ('Citi_Daily_Position',		'PARAM1'),
	# '2': ('Penjing_Monthy_Report',	'PARAM1'),
	# '3': ('PingAn_Monthly_Report',	'PARAM1'),
	# '4': ('ZH_Monthly_Report',		'PARAM1'),
	# '5': ('Daily_Report',				'PARAM2'),
	# '6': ('ZH_Weekly_Report',			'PARAM2')
# }

rpt_conf = {
	1: ('CSFB_Daily_Trade_Report',				'PARAM1'),
	2: ('Citi_Daily_Position',					'PARAM1'),
	3: ('Daily_Report',							'PARAM2'),
	4: ('Short_Report',							'PARAM1'),
	5: ('Penjing_Mid_Mth_Report',				'PARAM1'),
	6: ('Citi_Monthly_Position',				'PARAM1'),
	7: ('Penjing_Monthy_Report',				'PARAM1'),
	8: ('Month_End_Risk_Report',				'PARAM2'),
	9: ('Newsletter',							'PARAM1'),
	10: ('Monthly_Region_Expo_And_Attr',		'PARAM1'),
	11: ('Annual_PnL',							'PARAM2'),
	12: ('Realized_PnL',						'PARAM2'),
	13: ('Position_Limit_Monitoring_report',	'PARAM1'),
	14: ('Month_Trading_Summary_Report',        'PARAM4'),
	15: ('Monthly_Expo_And_Attr_Report',        'PARAM1'),
	16: ('QFIITaxAllocation',                   'PARAM1'),
	17: ('CPPIB_MTD_Report',                    'PARAM1'),
	18: ('CPPIB_Position',                      'PARAM1'),
	19: ('ActiveWeight',                        'PARAM1'),
	20: ('PingAn_Monthly_Report',               'PARAM1'),
	21: ('ZH_Monthly_Report',                   'PARAM1'),	
	22: ('ZH_Weekly_Report',                    'PARAM2'),
	23: ('SY_Monthly_Report',                   'PARAM1'),	
	24: ('ECB_Report',                          'PARAM1'),	
	25: ('MB6_Monthly_Report',                  'PARAM1'),
	26: ('Monthly_Holding_Report',              'PARAM2'),
	27: ('LS_Mkt_Exposure_Report',              'PARAM5'),
    28: ('JS151_Monthly_Report',                'PARAM1'),
    29: ('New_ZH_Monthly_Report',               'PARAM1'),	
	30: ('Monthly_Data_Collection',             'PARAM2')
}


def report_wrapper(rpt_routine_name, param_type):
	rpt_routine = getattr(rpt_m,rpt_routine_name)
	if rpt_routine:
		print 'start making report:', rpt_routine_name, '...'
	else:
		print 'ERROR: Unknown report routine,', rpt_routine_name
		
	if param_type=='PARAM2':
		date_str = raw_input('report params: report date(yyyymmdd): ')
		if len(date_str)!=8:
			print 'ERROR: NOT Required Date Format.'	
			return 
		rpt_date = datetime.strptime(date_str, '%Y%m%d').date()
		
		portf = raw_input('report params: portfolio ID, seperate by comma: ')
		portf_list = map(lambda x:x.upper(), portf.split(','))
		
		rpt_routine(rpt_date, portf_list)
		
	elif param_type=='PARAM3':
		rpt_routine()
		
	#modified by bing @20131102	
	elif param_type=='PARAM4':
		date_str = raw_input('report params: report date(yyyymmdd): ')
		if len(date_str)!=8:
			print 'ERROR: NOT Required Date Format.'	
			return 
		rpt_date = datetime.strptime(date_str, '%Y%m%d').date()
		
		portf = raw_input('report params: portfolio ID, seperate by comma: ')
		portf_list = map(lambda x:x.upper(), portf.split(','))
		tick = raw_input('report params: security ticker, seperate by comma: ')
		tick_list = map(lambda x:x.upper(), tick.split(','))

		if portf_list[0] == '':
			portf_list = None
		if tick_list[0] == '':
			tick_list = None
		
		rpt_routine(rpt_date, portf_list, tick_list)
		
	#modified by bing @20131127	
	elif param_type=='PARAM5':
		portf_id = raw_input('report params: portfolio ID: ')
		
		date_str = raw_input('report params: start date(yyyymmdd): ')
		if len(date_str)!=8:
			print 'ERROR: NOT Required Date Format.'	
			return 
		start_date = datetime.strptime(date_str, '%Y%m%d').date()
		
		date_str = raw_input('report params: end date(yyyymmdd): ')
		if len(date_str)!=8:
			print 'ERROR: NOT Required Date Format.'	
			return 
		end_date = datetime.strptime(date_str, '%Y%m%d').date()
		
		is_matched = raw_input('report params: is matched published nav(Y/N): ')
		if is_matched != 'Y' and is_matched != 'N':
			print '''error: input should be 'Y' or 'N'.'''	
			return
		is_matched = True if is_matched == 'Y' else False
		
		rpt_routine(portf_id, start_date, end_date, is_matched)
		
	else:
		date_str = raw_input('report params: report date(yyyymmdd): ')
		if len(date_str)!=8:
			print 'ERROR: NOT Required Date Format.'	
			return 
		rpt_date = datetime.strptime(date_str, '%Y%m%d').date()
		
		rpt_routine(rpt_date)
		

def do_reporting():
	#print dir(rpt_m)
	#return
		
	print 'report list: '
	print '---------------------------------'
	for item in sorted(rpt_conf.iteritems(), key=itemgetter(0)):
		print item[0],'|',item[1][0]
	
	print '---------------------------------'
	print ' '
	rpt_str = raw_input('select report ID, seperate by comma:')
	rpt_list = rpt_str.split(',')
	for id in rpt_list:
		id_num = int(id)
		if not rpt_conf.has_key(id_num):
			print 'ERROR: Unknown Report ID,', id
		
		report_wrapper(rpt_conf[id_num][0], rpt_conf[id_num][1])
		print '  '

		
if __name__ == '__main__':
	#if not check_usr_privilege():
	#	print 'Wrong User or Password...'
	#	exit(0)
		
	do_reporting()
	
	
	
